package com.sotb.sender;

import com.sotb.sender.MySlipSwitch.OnSwitchListener;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
 * 
 * @author lgb zsr
 * 设置界面类
 */
public class SettingActivity extends Activity {
	private MySlipSwitch record_slipswitch;
	private MySlipSwitch erperience_slipswitch;
	
	private Button stopSMSBtn = null;
	private Button recordingBtn = null;
	private Button helpBtn = null;
	private Button checkFriendBtn = null;
	
	boolean isRecord = false;
	boolean isExperience = true;
	boolean isStartService = false;
	SharedPreferences set = null;
	Editor editor = null;
	NotificationManager notificationManager;
	Notification notification;
	Intent startServiceIntent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_layout);
		//获得应用程序共享的preference
        set = getSharedPreferences(Constant.PREFERENCE_NAME, MODE_PRIVATE);
        //获得preference的editer
    	editor = set.edit();
    	
    	record_slipswitch = (MySlipSwitch)findViewById(R.id.record_slipswitch);
        erperience_slipswitch = (MySlipSwitch)findViewById(R.id.erperience_slipswitch);
        
        stopSMSBtn = (Button)findViewById(R.id.stop_sms);
        recordingBtn = (Button) findViewById(R.id.recording);
        helpBtn = (Button) findViewById(R.id.help);
        checkFriendBtn = (Button) findViewById(R.id.check_friend);

        //读取设置
        isRecord = set.getBoolean(Constant.IS_RECORD, false);
        isExperience = set.getBoolean(Constant.IS_EXPERIENCE, true);
        isStartService = set.getBoolean(Constant.IS_START_SERVICE, false);
        
        set = getSharedPreferences(Constant.PREFERENCE_NAME, MODE_WORLD_READABLE);
    	int content = set.getInt("a", 1 );
        if(content == 1){
            SharedPreferences.Editor editor = getSharedPreferences(Constant.PREFERENCE_NAME,MODE_WORLD_WRITEABLE).edit();
            editor.putInt("a",2);
            editor.commit();
            System.out.println("content2---->"+content);
            Intent intent = new Intent(SettingActivity.this, HelpActivity.class);
            startActivity(intent);
        }   

        startServiceIntent = new Intent();
        defineNotificationManager();
       
		
		checkFriendBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(SettingActivity.this, FriendsActivity.class);
				startActivity(intent);
			}
		});
		
		
        //是否录音checkBox设置
		record_slipswitch.setImageResource(R.drawable.bkg_switch, R.drawable.bkg_switch, R.drawable.btn_slip);
        record_slipswitch.setSwitchState(true);
        record_slipswitch.setOnSwitchListener(new OnSwitchListener() {
			
			//@Override
			public void onSwitched(boolean isSwitchOn) {
				// TODO Auto-generated method stub
				if(isSwitchOn) {
					isRecord = true;
				} else {
					isRecord = false;
				}
				editor.putBoolean(Constant.IS_RECORD, isRecord);
			}
		});
        
        //是否体验checkBox设置
        erperience_slipswitch.setImageResource(R.drawable.bkg_switch, R.drawable.bkg_switch, R.drawable.btn_slip);
        erperience_slipswitch.setSwitchState(true);
        erperience_slipswitch.setOnSwitchListener(new OnSwitchListener() {
			
			//@Override
			public void onSwitched(boolean isSwitchOn) {
				// TODO Auto-generated method stub
				if(isSwitchOn) {
					isExperience = true;
				} else {
					isExperience = false;
				}
				editor.putBoolean(Constant.IS_EXPERIENCE, isExperience);
			}
		});
        
      
        //停止发送短息
        stopSMSBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(SettingActivity.this, SMSSenderService.class);
				stopService(intent);
			}
		});
        
        recordingBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(SettingActivity.this, Recording.class);
				startActivity(intent);
			}
		});
        
        helpBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				editor.putInt("a", 1);
				Intent intent = new Intent();
				intent.setClass(SettingActivity.this, HelpActivity.class);
				startActivity(intent);
			}
		});
	}
	
	
	private void defineNotificationManager(){
		//获得系统NM服务
		String service = NOTIFICATION_SERVICE;
		notificationManager = (NotificationManager)getSystemService(service);
		//实例化Notification对象，并设置其属性
		notification = new Notification(R.drawable.ic_launcher, "开启静鸡鸡报警服务", System.currentTimeMillis());
		Intent intent = new Intent(this, SettingActivity.class);
		PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
		notification.setLatestEventInfo(this, "遇到危险时可连续按5下电源键", "静鸡鸡报警正在后台运行，时刻保障您的安全", pi);
		
        startServiceIntent.setClass(SettingActivity.this, SenderService.class);
        startService(startServiceIntent);
		notificationManager.notify(Constant.NOTIFICATION_ID, notification);
		System.out.println("start service");
	}
	
	
	/*保存设置*/
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		if (editor != null){
			editor.commit();
		}		
		super.onPause();
	}
	
	
	/*读取设置*/
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		//读取设置
        isRecord = set.getBoolean(Constant.IS_RECORD, false);
        isExperience = set.getBoolean(Constant.IS_EXPERIENCE, true);
        isStartService = set.getBoolean(Constant.IS_START_SERVICE, false);
        //读取checkBox设置
        record_slipswitch.setSwitchState(isRecord);
        erperience_slipswitch.setSwitchState(isExperience);
		super.onResume();
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, Constant.EXIT, 0, "退出");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case Constant.EXIT:{
			stopService(startServiceIntent);
			notificationManager.cancel(Constant.NOTIFICATION_ID);
			System.out.println("stop service");
			//退出程序
			finish();
		}break;
		}
		return true;
	}
	
	//取消返回键
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME){
			
		}
		return false;
	}
}
